enum SpeedUnit {
KMH, MS
}
class Speed {
public double value;
public SpeedUnit unit;
}
class SpaceShip {
private Speed speed;
public SpaceShip(){
this.speed = new Speed();
this.speed.unit = SpeedUnit.MS;
}
public void accelerate(Speed delta){
if(delta.unit == this.speed.unit && delta.value > 0) {
this.speed.value += delta.value;
}
}
}
class LandVehicle {
private Speed speed;
public LandVehicle(){
this.speed = new Speed();
this.speed.unit = SpeedUnit.KMH;
}
public void accelerate(Speed delta){
if(delta.unit == this.speed.unit) {
this.speed.value += delta.value;
}
}
}
class Speed {
private double value;
private SpeedUnit unit;
public Speed(double value, SpeedUnit unit){
this.value = value;
this.unit = unit;
}
public Speed add(Speed that){
if(this.unit == that.unit){
return new Speed(this.value + that.value, this.unit);
} else {
return this; // lepiej rzucić wyjątek lub dokonać konwersji
}
}
public boolean isAboveZero(){
return value > 0;
}
}
class SpaceShip {
private Speed speed;
public SpaceShip(){
this.speed = new Speed(0, SpeedUnit.MS);
}
public void accelerate(Speed delta){
if(delta.isAboveZero()) {
this.speed = this.speed.add(delta);
}
}
}
class LandVehicle {
private Speed speed;
public LandVehicle(){
this.speed = new Speed(0, SpeedUnit.KMH);
}
public void accelerate(Speed delta){
this.speed = this.speed.add(delta);
}
}
Speed speed0 = new Speed(10, SpeedUnit.KMH);
speed0.toString();
REPL.$JShell$17BCS$Speed@7cdb2ef8
class Speed {
private double value;
private SpeedUnit unit;
public Speed(double value, SpeedUnit unit){
this.value = value;
this.unit = unit;
}
@Override // <- nie jest wymagane, żeby zadziałało
public String toString(){
return "" + value + " " + unit;
}
}
Speed speed1 = new Speed(10, SpeedUnit.KMH); // <---- Speed
speed1.toString();
10.0 KMH
Object speed2 = new Speed(10, SpeedUnit.KMH); // <---- Object!
speed2.toString();
10.0 KMH
class Speed {
private double value;
private SpeedUnit unit;
public Speed(double value, SpeedUnit unit){
this.value = value;
this.unit = unit;
}
@Override
public String toString(){
return super.toString() + " " + value + " " + unit;
}
}
Speed speed1 = new Speed(10, SpeedUnit.KMH);
speed1.toString();
REPL.$JShell$17FT$Speed@3e37b0bf 10.0 KMH
enum SpeedUnit {
MS, KMH, MA
}
class Speed {
public double value = 0;
public SpeedUnit unit = SpeedUnit.MS;
}
class SuperSonicSpeed extends Speed {
public double value = 10;
public SpeedUnit unit = SpeedUnit.MA;
}
Speed speed1 = new Speed();
System.out.println(speed1.unit);
System.out.println(speed1.value);
MS 0.0
Speed speed2 = new SuperSonicSpeed(); // <--- zmienna typu Speed
System.out.println(speed2.unit);
System.out.println(speed2.value);
MS 0.0
SuperSonicSpeed speed3 = new SuperSonicSpeed(); // <--- zmienna typu SuperSonicSpeed
System.out.println(speed3.unit);
System.out.println(speed3.value);
MA 10.0
private
¶class Speed {
protected static final double MS_TO_KMH_RATIO = 3.6;
protected double value;
protected SpeedUnit unit;
public Speed(double value, SpeedUnit unit){
this.value = value;
this.unit = unit;
}
public Speed add(Speed that){
return new Speed(this.value + that.convert(this.unit), this.unit);
}
public Speed subtract(Speed that){
return new Speed(this.value - that.convert(this.unit), this.unit);
}
public String toString() {
return "" + this.value + " " + this.unit;
}
private double convert(SpeedUnit target){
if(this.unit == SpeedUnit.KMH && target == SpeedUnit.MS){
return this.value / MS_TO_KMH_RATIO;
} else if(this.unit == SpeedUnit.MS && target == SpeedUnit.KMH){
return this.value * MS_TO_KMH_RATIO;
} else {
// pozostałe przypadki
return this.value;
}
}
}
Speed speed1 = new Speed(10, SpeedUnit.MS);
Speed speed2 = new Speed(36, SpeedUnit.KMH);
speed2.add(speed1);
72.0 KMH
speed1.convert(SpeedUnit.MS);
| speed1.convert(SpeedUnit.MS); convert(SpeedUnit) has private access in Speed
class SuperSonicSpeed extends Speed {
public SuperSonicSpeed(double value){
super(value, SpeedUnit.MA);
}
public Speed subtract(SuperSonicSpeed that){
return new Speed(this.value - that.convert(this.unit), this.unit);
}
public double convert(SpeedUnit target){
return 0;
}
}
SuperSonicSpeed speed1 = new SuperSonicSpeed(10);
SuperSonicSpeed speed2 = new SuperSonicSpeed(10);
speed1.add(speed2);
20.0 MA
SuperSonicSpeed speed1 = new SuperSonicSpeed(10);
SuperSonicSpeed speed2 = new SuperSonicSpeed(10);
speed1.subtract(speed2);
10.0 MA
protected
¶class Speed {
private static final double MS_TO_KMH_RATIO = 3.6;
private double value;
private SpeedUnit unit;
public Speed(double value, SpeedUnit unit){
this.value = value;
this.unit = unit;
}
public Speed add(Speed that){
return new Speed(this.value + that.convert(this.unit), this.unit);
}
public Speed subtract(Speed that){
return new Speed(this.value - that.convert(this.unit), this.unit);
}
public String toString() {
return "" + this.value + " " + this.unit;
}
protected double convert(SpeedUnit target){
if(this.unit == SpeedUnit.KMH && target == SpeedUnit.MS){
return this.value / MS_TO_KMH_RATIO;
} else if(this.unit == SpeedUnit.MS && target == SpeedUnit.KMH){
return this.value * MS_TO_KMH_RATIO;
} else {
return this.value;
}
}
}
class SuperSonicSpeed extends Speed {
public SuperSonicSpeed(double value){
super(value, SpeedUnit.MA);
}
protected double convert(SpeedUnit target){
return 0;
}
}
Speed speed1 = new SuperSonicSpeed(10);
Speed speed2 = new SuperSonicSpeed(10);
speed1.add(speed2);
10.0 MA
final
¶class Speed {
protected final double convert(SpeedUnit target){
if(this.unit == SpeedUnit.KMH && target == SpeedUnit.MS){
return this.value / MS_TO_KMH_RATIO;
} else if(this.unit == SpeedUnit.MS && target == SpeedUnit.KMH){
return this.value * MS_TO_KMH_RATIO;
} else {
return this.value;
}
}
}
class SuperSonicSpeed extends Speed{
public SuperSonicSpeed(double value){
super(value, SpeedUnit.MA);
}
protected double convert(SpeedUnit target){
return 0;
}
}
| super(value, SpeedUnit.MA); constructor Speed in class Speed cannot be applied to given types; required: no arguments found: double,SpeedUnit reason: actual and formal argument lists differ in length | protected double convert(SpeedUnit target){ | return 0; | } convert(SpeedUnit) in SuperSonicSpeed cannot override convert(SpeedUnit) in Speed overridden method is final
static
(metody klasowe)¶class Speed {
private double value = 10;
public String toString(){
return this.value + "";
}
public void run(){
main(null);
}
public static void main(String[] args){
Speed speed1 = new Speed();
System.out.println(speed1);
}
}
Speed.main(null);
10.0
Speed speed = new Speed();
speed.run();
10.0
class Speed {
private final double value;
private static Map<Double, Speed> instances = new HashMap<>();
private Speed(double value){
this.value = value;
}
public static Speed create(double value){
if(instances.containsKey(value)){
return instances.get(value);
} else {
Speed instance = new Speed(value);
instances.put(value, instance);
return instance;
}
}
}
Speed speed1 = Speed.create(10);
Speed speed2 = Speed.create(10);
speed1 == speed2
true
class Speed {
private double value;
private static Map<Double, Speed> instances = new HashMap<>();
protected Speed(double value){
this.value = value;
}
public String toString(){
return "" + this.value;
}
public static Speed create(double value){
if(instances.containsKey(value)){
return instances.get(value);
} else {
Speed instance = createInstance(value);
instances.put(value, instance);
return instance;
}
}
protected static Speed createInstance(double value){
return new Speed(value);
}
}
class SuperSonicSpeed extends Speed {
private SuperSonicSpeed(double value){
super(value);
}
protected static SuperSonicSpeed createInstance(double value){
return new SuperSonicSpeed(value * 10);
}
}
Speed speed = SuperSonicSpeed.create(10);
speed
10.0
class Speed {
private double value;
// modyfikator domyślny dla konstruktora
Speed(double value){
this.value = value;
}
}
class SpeedFactory {
private Map<Double, Speed> instances = new HashMap<>();
public Speed create(double value){
if(instances.containsKey(value)){
return instances.get(value);
} else {
Speed instance = new Speed(value);
instances.put(value, instance);
return instance;
}
}
}
SpeedFactory factory = new SpeedFactory();
Speed speed1 = factory.create(10);
Speed speed2 = factory.create(10);
speed1 == speed2;
true
class Speed {
public static Speed toMs(Speed speed1){
return new Speed(speed1.convert(SpeedUnit.MS), SpeedUnit.MS);
}
}
Speed speed1 = new Speed(10, SpeedUnit.MS);
Speed.toMs(speed1);
// vs.
speed1.toMs();
abstract
¶abstract class AbstractSpaceShip {
private Speed speed;
public AbstractSpaceShip(){
this.speed = new Speed(0, SpeedUnit.MS);
}
public abstract void start();
public abstract void land();
}
class Rocket extends AbstractSpaceShip {
private Engine engine;
private Parachute parachute;
public void start(){
engine.start();
}
public void land(){
parachute.open();
}
}
class SpaceShuttle extends AbstractSpaceShip {
private List<Booster> boosters;
public void start(){
for(Booster booster : boosters){
booster.start();
}
}
public void land(){
descend(new Distance(10, DistanceUnit.KM));
}
private void descend(Distance distance){
//...
}
}
// aliens are coming...
List<AbstractSpaceShip> spaceFleet = new LinkedList<>();
spaceFleet.add(new Rocket());
spaceFleet.add(new SpaceShuttle());
for(AbstractSpaceShip ship : spaceFleet){
ship.start();
}
// rescue the World, then...
for(AbstractSpaceShip ship : spaceFleet){
ship.land();
}
interface ISpaceShip {
void start();
void land();
}
class Rocket implements ISpaceShip {
public void start(){
//...
}
public void land(){
//...
}
}
class SpaceShuttle implements ISpaceShip {
public void start(){
//...
}
public void land(){
//...
}
}
// aliens are coming...
List<ISpaceShip> spaceFleet = new LinkedList<>();
spaceFleet.add(new Rocket());
spaceFleet.add(new SpaceShuttle());
for(AbstractSpaceShip ship : spaceFleet){
ship.start();
}
// rescue the World, then...
for(AbstractSpaceShip ship : spaceFleet){
ship.land();
}
| for(AbstractSpaceShip ship : spaceFleet){ incompatible types: ISpaceShip cannot be converted to AbstractSpaceShip
interface ISpaceShip {
void start();
void land();
// nowa metoda
void launchMissile();
}
default
¶interface ISpaceShip {
void start();
void land();
// nowość w Javie 8.0
default void launchMissile(){
InterplanetarySystem.out.
println("Włamujemy się emacsem przez sendmeila");
InterplanetarySystem.out.
println("omijając potrójną ścianę ogniową");
}
}
abstract class Car {
public void openDoor(DoorSpecification doorSpec){
doors.get(doorSpec).open();
}
public abstract void drive();
}
class GasolineCar extends Car {
private GasolineEngine engine;
public void drive(){
engine.deliverGasoline();
engine.igniteGasoline();
//...
}
}
class ElectricCar extends Car {
private List<WheelMotor> motors;
public void drive(){
for(WheelMotor motor : motors){
motor.rotate();
}
}
}
class Car {
private IDriveSystem driveSystem;
public Car(IDriveSystem driveSystem){
this.driveSystem = driveSystem;
}
public drive(){
this.driveSystem.drive();
}
}
interface IDriveSystem {
void drive();
}
class ElectricDriveSystem implements IDriveSystem {
//...
}
class GasolineDriveSystem implements IDriveSystem {
//...
}
Car car = new Car(new GasolineDriveSystem());